package cwinsor.com.leaf_raking;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import org.w3c.dom.NamedNodeMap;



import org.w3c.dom.NodeList;


/**
 * @author cwinsor
 *
 */
public class ApptList {

	// the list of rakers
	private ArrayList<Appointment> appointmentList;

	// debug
	String dS = this.getClass().getSimpleName();

	// debug
	private int debugSection = 1;

	/**
	 * constructor
	 */
	public ApptList() {

		appointmentList = new ArrayList<Appointment>();

	}

	/*
	 * get the appointment list
	 */
	public ArrayList<Appointment> getList() {
		return appointmentList;
	}



	/*
	 * read in data from the csv file and internalize into a list
	 */
	public void readCsvFile(File file) {

		System.out.println(dS + "reading csv file...");

		BufferedReader br = null;
		String line = "";
		String cvsSplitBy = ",";

		try {

			br = new BufferedReader(new FileReader(file));

			while ((line = br.readLine()) != null) {

				Msg.note(dS + ": (bef ) line in: " + line);
				line = line.replaceAll("[\n\r]", "#1#");
				//				line = line.replace("\n", "#1#").replace("\r", "#2#");
				Msg.note(dS + ": (aft1) line in: " + line);
				//			line = line.replace(System.getProperty("line.separator"), "#3#");
				//			Msg.note(dS + ": (aft2) line in: " + line);

				// read data into a string array, use comma as separator
				String[] theFields = line.split(cvsSplitBy);

				// convert to an ArrayList object
				ArrayList<String> theFieldsArrayObject = new ArrayList<String>();
				for (String s : theFields) {
					theFieldsArrayObject.add(s);
				}

				// skip the first line of input (header)
				if (theFieldsArrayObject.get(0).toString().equals("Start time")) {
					; // do nothing
				} else {
					// debug print
					if (debugSection  == 1) {
						Msg.note(dS + ": --------- input line ----------");
						for (int i=0; i<theFieldsArrayObject.size(); i++) {
							Msg.note(dS + ": "
									+ i
									+ "  \"" + theFieldsArrayObject.get(i).toString() + "\"");
						}
					}

					// add an appointment
					Appointment appointment = new Appointment();
					appointment.addData(theFieldsArrayObject);
					appointmentList.add(appointment);
				}
			}
			System.out.println(dS + "there are " + appointmentList.size() + " appointments");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		System.out.println(dS + "reading csv file DONE");

		// check
		this.check();
	}


	/*
	 * print the data to screen
	 */
	public void check() {
		System.out.println(dS + "checking... ");

		for (Appointment c: appointmentList) {
			if (c.getTeam() == TeamNumber.TEAM_UNASSIGNED) {
				System.out.println(dS
						+ "Appointment "
						+ c.getName()
						+ " is NOT ASSIGNED TO A TEAM !!!");
			}
		}

		System.out.println(dS + "checking done ");
	}


	/*
	 * print the data to screen
	 */
	public void print() {
		Iterator<Appointment> iterator = appointmentList.iterator();
		while (iterator.hasNext()) {
			Appointment c = iterator.next();
			System.out.println(dS + c.toStringCommaDelimitedAllFields());
		}
	}

	/*
	 * return a String of all contents in commma-separated-value format
	 */
	public String toCsvString() {
		String in = "";

		// debug print
		if (debugSection == 3) {
			System.out.println(dS + " appointmentList.size = " + appointmentList.size());
		}
		Iterator<Appointment> iterator = appointmentList.iterator();
		while (iterator.hasNext()) {
			Appointment c = iterator.next();

			in = in
					//+ c.getEmail().toString()

					+ "," + "CUSTOMER"
					// + "," + c.getDay().name()
					+ "," + c.getStartTime().name()
					// + "," + c.getShift().name()
					+ "," + c.getDuration()
					+ "," + c.getName().toString()
			
					+ ",(h)" + c.getPhone().toString()

					+ "," + c.getAddress().toString()
					+ "," + c.getSizeOfArea().toString()
					+ "," + c.getNotes().toString();
			in = in + "\n";
		}
		// debug print
		if (debugSection == 3) {
			System.out.println(dS + "---- toCsvString...  done");
		}
		return in;
	}


	/*
	 * return an AppointmentList given shift and team
	 */
	public ApptList getCustomersDayShiftTeam(WorkShift workShift, TeamNumber teamNumber) {
		ApptList theList = new ApptList();

		// for each entry in the list
		for (Appointment appointment : appointmentList) {
			if (appointment.getShift().equals(workShift) && appointment.getTeam().equals(teamNumber)) {
				theList.addCustomer(appointment); 
			}
		}
		return theList;	
	}

	/* 
	 * add a customer to the list
	 * 
	 */
	private void addCustomer(Appointment c) {
		appointmentList.add(c);
	}

	/*
	 * count
	 * returns the number of rakers in the internal raker list
	 */
	public int count() {
		return appointmentList.size();
	}

	/*
	 * sort by start time
	 */
	public void sort() {

		// Sort by start time
		Collections.sort(appointmentList, new Appointment());
	}


}